package com.wtwd.wtwddevice.listener.appMsg;

import com.alibaba.fastjson.JSONArray;
import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.constant.SetSosConstant;
import com.wtwd.device.model.AppSetSos;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.SetSos;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.ISetSosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * app设置sos
 */
@MsgType(MsgTypeEnum.APP_W_SOS)
@Component
public class AppSetSosHandlerImpl implements IMsgHandler {
    @Autowired
    private ISetSosService setSosService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private PushAppMsgService msgService;


    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        String number = deviceSendService.generateNumber(appProtocol.getImei());
        Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
        JSONArray json = (JSONArray) appProtocol.getData();
        List<AppSetSos> list = json.toJavaList(AppSetSos.class);
        String body = AppSetSos.serialization(list);
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(MsgTypeEnum.W_SOS.getCmdName())
                .body(body)
                .bodyLength(body.length())
                .number(number)
                .build();
        if (deviceSendService.sendMsgDevice(wtProtocol)){//如果为fase直接告知app端设置失败
            //多个有删除 有修改
            List<SetSos> setSos = new ArrayList<>();
            for(int i = 0;i < list.size();i++){
                SetSos sos = new SetSos();
                AppSetSos appSetSos = list.get(i);
                sos.setDeviceId(deviceId);
                sos.setGender(Byte.valueOf(appSetSos.getGender()));
                sos.setNickName(appSetSos.getNickName());
                sos.setTelephone(appSetSos.getTelephone());
                if(appSetSos.getClean() == null || !appSetSos.getClean().equals(SetSosConstant.TRUE_CLEAN)){
                    sos.setClean(SetSosConstant.FALSE_CLEAN);
                }
                sos.setNumber(number);
                sos.setTime(wtProtocol.getTime());
                sos.setUpdateTime(System.currentTimeMillis());
                sos.setCreateTime(System.currentTimeMillis());
                sos.setState(CmdStateEnum.SENDING.getState());

                setSos.add(sos);
            }
            setSosService.batchEditSetSosButton(setSos);
        }else {//
            AppProtocol protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.W_SOS.getCmdName())
                    .state(AnswerStateEnum.ABNORMAL.getState())
                    .data(null)
                    .build();
            msgService.pushToDevice(messageBase.getForm(),
                    MessageBase.builder()
                            .appProtocol(protocol)
                            .build()
            );
        }
    }
}
